package com.hsnn.medstgmini.sys.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.sys.dao.PassWordDao;
import com.hsnn.medstgmini.sys.model.SysUser;


@Repository
public class PassWrodDaoImpl implements PassWordDao{
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	
	
	/**
	 * @category 根据id查询用户
	 * @date 2015年6月29日
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SysUser getUserById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(SysUser.class);
		criteria.add(Restrictions.eq("id", id));
		List<SysUser> list = criteria.list();
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	/**
	 * @category 密码修改
	 * @date 2015年6月29日
	 * @param id
	 * @return
	 */
	public void changePassword(String id, String password) {
		Session session = sessionFactory.getCurrentSession();
		if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(password)) {
			SysUser user = getUserById(id);
			if (user != null) {
				user.setPassword(password);
				session.saveOrUpdate(user);
				session.flush();
			}
		}
	}
	

}
